/*
 * FileLoader.cpp
 *
 *  Created on: 11/10/2011
 *      Author: gisedaye
 */

#include "FileLoader.h"
bool FileLoader::fileExists(string filename){
	ifstream file;
	file.open(filename.c_str(),fstream::in|fstream::binary);
	if(!file.is_open()){
		return false;
	}else{
		file.close();
		return true;
	}
}
bool FileLoader::createFile(string filename) {
	fstream file;
	file.open(filename.c_str(), fstream::in | fstream::out | fstream::trunc | fstream::binary);
	if (file.is_open()) {
		file.close();
		return true;
	}
	return false;
}
long FileLoader::getHashTableSize(string hashTableFilename){
	ifstream hashTableFile(hashTableFilename.c_str(),ios::in|ios::binary);
	long beg, end;
	beg = hashTableFile.tellg();
	hashTableFile.seekg (0, ios::end);
	end = hashTableFile.tellg();
	hashTableFile.close();
	return (end-beg);
}

string FileLoader::getBucketNumberFromFile(string filename,int dispersionNumber){

	string data=FileLoader::readFromFile(filename,dispersionNumber,sizeof(unsigned int));
	return data;
}

string FileLoader::getBucketFromFile(string filename,int bucketNumber){
	long start =(bucketNumber * BUCKET_SIZE );
	string data=FileLoader::readFromFile(filename,start,BUCKET_SIZE);
	return data;
}

string FileLoader::readFromFile(string filename,int start, int size){
	ifstream file(filename.c_str(),ios::in|ios::binary);

	file.seekg(start, ios_base::beg);
	char * buffer = new char[size];
	memset(buffer, 0, size);
	file.read(buffer, size);
	string buff=(string)buffer;

	file.close();

	return buff;
}
